package com.example.unzip;

import com.github.junrar.Archive;
import com.github.junrar.UnrarCallback;
import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader;
import com.github.junrar.volume.Volume;

import java.io.*;
import java.nio.charset.Charset;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtil {

    public static void main(String[] args) throws Exception{

        //String filePath = "F:\\test.zip";
        //unzip2(filePath);
        File file = new File("D:\\xrb"+File.separator+"test");
        if(!file.exists()){
            file.mkdirs();
        }
        System.out.println(file.getAbsolutePath());
        file.delete();
    }

    public void zip(){

    }

    public static void unzip2(String filePath){
        try {
            ZipInputStream Zin=new ZipInputStream(new FileInputStream(filePath), Charset.forName("GBK"));//输入源zip路径
            BufferedInputStream Bin=new BufferedInputStream(Zin);
            String Parent="F:\\ziptest\\"; //输出路径（文件夹目录）
            File Fout=null;
            ZipEntry entry;
            try {
                while((entry = Zin.getNextEntry())!=null){
                    System.out.println(entry.getName());
                   /* Fout=new File(Parent,entry.getName());
                    if(!Fout.exists()){
                        (new File(Fout.getParent())).mkdirs();
                    }
                    FileOutputStream out=new FileOutputStream(Fout);
                    BufferedOutputStream Bout=new BufferedOutputStream(out);
                    int b;
                    while((b=Bin.read())!=-1){
                        Bout.write(b);
                    }
                    Bout.close();
                    out.close();
                    System.out.println(Fout+"解压成功");*/
                }
                Bin.close();
                Zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void unzip(String filePath) throws Exception{
        try {
            ZipInputStream Zin=new ZipInputStream(new FileInputStream(filePath), Charset.forName("GBK"));//输入源zip路径
             BufferedInputStream Bin=new BufferedInputStream(Zin);
            String Parent="F:\\ziptest\\"; //输出路径（文件夹目录）
             File Fout=null;
            ZipEntry entry;
            try {
                while((entry = Zin.getNextEntry())!=null){
                    Fout=new File(Parent,entry.getName());
                    if(!Fout.exists()){
                        (new File(Fout.getParent())).mkdirs();
                    }
                    FileOutputStream out=new FileOutputStream(Fout);
                    BufferedOutputStream Bout=new BufferedOutputStream(out);
                    int b;
                    while((b=Bin.read())!=-1){
                        Bout.write(b);
                    }
                    Bout.close();
                    out.close();
                    System.out.println(Fout+"解压成功");
                }
                Bin.close();
                Zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}
